// Copyright (c) 2012 Rene Bolldorf <rb@radix.io>
// Use of this source is governed by the GNU General Public License version 3,
// the license can be found in the LICENSE file.

var util = {};

util.untilTo = function (future) {
  var future = new Date(0).setUTCSeconds(future);
  var now = new Date().getTime()
  var diff = future - now;

  if (diff <= 0)
    return ' already completed';

  var seconds = Math.floor(diff / 1000);
  var minutes = Math.floor(seconds / 60);
  var hours = Math.floor(minutes / 60);
  var days = Math.floor(hours / 24);
  var str = '';

  hours %=24;
  minutes %=60;
  seconds %= 60;

  if (days > 0)
    str += days + ' d ';
  if (hours > 0)
    str += hours + ' h ';
  if (minutes > 0)
    str += minutes + ' m ';
  if (seconds > 0)
    str += seconds + ' s';

  return str;
};
